package com.xnyzc.lhy.order.util;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.util.CheckUtil;

import java.util.List;

/**
 * @author: yangjc
 * @since: 2019-05-21 17:40
 */
public class PageUtils {

    public static <T> Page<T> createPage(List<T> list, Page page) {
        if (list == null || page == null || CheckUtil.objIsEmpty(page.getCurrent(), page.getSize())) {
            return null;
        }
        if (page.getCurrent() < 1) {
            throw PangException.create(EErrorCode.noData, "当前页不能小于1");
        }
        long startIndex = (page.getCurrent() - 1) * page.getSize();
        long endIndex = (page.getCurrent()) * page.getSize();

        Page<T> tPage = new Page<>();
        tPage.setTotal(list.size());
        tPage.setCurrent(page.getCurrent());
        tPage.setSize(page.getSize());
        if (endIndex > list.size()) {
            tPage.setRecords(list.subList((int) startIndex, list.size()));
        } else {
            tPage.setRecords(list.subList((int) startIndex, (int) endIndex));
        }

        return tPage;
    }
}
